/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aop.framework.adapter;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.Interceptor;
import org.springframework.aop.Advisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;

import java.util.LinkedList;
import java.util.List;

/**
 * 此类默认注册了3个适配器(在构造函数里),当调用getInterceptor时会临时创建出Interceptor对象出来
 *
 * @author Rod Johnson
 * @version $Id: DefaultAdvisorAdapterRegistry.java,v 1.10 2004/03/19 16:54:41 johnsonr Exp $
 */
public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry {

  private List adapters = new LinkedList();

  public DefaultAdvisorAdapterRegistry() {
    // register well-known adapters
    registerAdvisorAdapter(new BeforeAdviceAdapter());
    registerAdvisorAdapter(new AfterReturningAdviceAdapter());
    registerAdvisorAdapter(new ThrowsAdviceAdapter());
  }

  /**
   * spring 框架内部主要是靠Interceptor,具体点就是MethodInterceptor来实现AOP的
   * 核心逻辑
   * 1. 如果是Advisor,不用包装
   * 2. 如果不是Advice,直接抛异常,必须是Advice或Advisor
   * 3.如果是Interceptor,那么不需要适配器,spring框架支持它,直接用DefaultPointcutAdvisor包装此拦截器
   * 4.如果不是Interceptor而只是一个Advice,那么需要有对应的适配器,否则也抛异常
   * 因为适配器知道这个Advice怎么执行.请参看BeforeAdviceAdapter返回的MethodBeforeAdviceInterceptor
   * @param adviceObject
   * @return
   * @throws UnknownAdviceTypeException
   */
  public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException {
    if (adviceObject instanceof Advisor) {
      return (Advisor) adviceObject;
    }

    if (!(adviceObject instanceof Advice)) {
      throw new UnknownAdviceTypeException(adviceObject);
    }
    Advice advice = (Advice) adviceObject;

    if (advice instanceof Interceptor) {
      // So well-known it doesn't even need an adapter
      return new DefaultPointcutAdvisor(advice);
    }
    for (int i = 0; i < this.adapters.size(); i++) {
      // Check that it is supported
      AdvisorAdapter adapter = (AdvisorAdapter) this.adapters.get(i);
      if (adapter.supportsAdvice(advice)) {
        return new DefaultPointcutAdvisor(advice);
      }
    }
    throw new UnknownAdviceTypeException(advice);
  }

	public Interceptor getInterceptor(Advisor advisor) throws UnknownAdviceTypeException {
		Advice advice = advisor.getAdvice();
		if (advice instanceof Interceptor) {
			return (Interceptor) advice;
		}
		for (int i = 0; i < this.adapters.size(); i++) {
			AdvisorAdapter adapter = (AdvisorAdapter) this.adapters.get(i);
			if (adapter.supportsAdvice(advice)) {
				return adapter.getInterceptor(advisor);
			}
		}
		throw new UnknownAdviceTypeException(advisor.getAdvice());
	}

	public void registerAdvisorAdapter(AdvisorAdapter adapter) {
		this.adapters.add(adapter);
	}

}
